package com.zhxy.manager.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhxy.manager.dao.DormitoryDao;
import com.zhxy.manager.entity.Course;
import com.zhxy.manager.entity.Dormitory;
import com.zhxy.manager.excel.AssociationExcel;
import com.zhxy.manager.excel.DormitoryExcel;
import com.zhxy.manager.service.intf.DormitoryService;
import com.zhxy.manager.vo.PageBean;
import com.zhxy.manager.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 宿舍人员管理
 *
 * @author ZM
 */
@Service
public class DormitoryServiceImpl implements DormitoryService {
    @Autowired
    private DormitoryDao dao;

    //新增
    @Override
    public R save(Dormitory dormitory) {
        if (dao.add(dormitory) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    //查询
    @Override
    public PageBean queryPage(int page, int limit) {
        //分页
        PageHelper.startPage(page,limit);
        //获取分页信息
        PageInfo<Dormitory> pageInfo = new PageInfo<>(dao.selectAll());
        //返回
        return PageBean.bean(pageInfo.getTotal(),pageInfo.getList());
    }

    //根据id 删除
    @Override
    public R del(int id) {
        if (id > 0) {
            if (dao.del(id) > 0) {
                return R.ok();
            } else {
                return R.fail();
            }
        } else {
            return R.fail();
        }
    }

    //修改
    @Override
    public R update(Dormitory dormitory) {
        if (dao.update(dormitory) > 0) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    //查询
    @Override
    public PageBean queryPageWhere(String name, int page, int limit) {
        //验证查询条以  如果为 " "  那么赋值null
        if (!StringUtils.hasLength(name)) {
            name = null;
        } else {
            name = "%"+ name +"%"; //拼接上模糊查询
        }
        //分页
        PageHelper.startPage(page, limit);
        //获取分页结果结果
        PageInfo<Dormitory> pageInfo = new PageInfo<>(dao.selectWhere(name));
        //返回分页数据
        return PageBean.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    //到出数据
    @Override
    public List<Dormitory> queryAll() {
        return dao.selectAll();
    }

    //批量删除
    @Override
    public R delBatch(int[] ids) {
        if (ids != null && ids.length > 0) {
            //执行
            if (dao.delBatch(ids) > 0) {
                return R.ok();
            }
        }
        return R.fail();
    }

    //批量导入
    @Override
    public R saveBatch(MultipartFile file) throws IOException {
        if(!file.isEmpty()){
            //excel解析
            List<DormitoryExcel> list= EasyExcel.read(file.getInputStream(), DormitoryExcel.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {}
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {}
            }).sheet().doReadSync();
            System.out.println(list);
            //执行dao 批量新增
            if(dao.addBatch(list)>0){
                return R.ok();
            }
        }
        return R.fail();
    }
}
